package com.tietiao.common.util;

import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.tietiao.common.constants.TietiaoConstants;
import com.tietiao.common.ftl.FtlPath;

import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class SendEmailClient {
	private static String from = TietiaoConstants.SEND_MAIL_FROM;
	private static String password = TietiaoConstants.SEND_MAIL_PASSWORD;
	private static String showName = TietiaoConstants.SEND_MAIL_SHOWNAME;
	private static Configuration cfg = null;
	static {
		cfg = new Configuration();
		cfg.setTemplateLoader(new ClassTemplateLoader(FtlPath.class, ""));
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
		cfg.setEncoding(Locale.CHINA, "UTF-8");
	}

	/**
	 * 给管理员发送邮件
	 * 
	 * @param title
	 *            标题
	 * @param body
	 *            邮件内容
	 * @return
	 */
	public static boolean sendToAdmin(String title, String body) {
		return sendEmail("whh32@qq.com", title, body);

	}

	/**
	 * 发送邮件
	 * 
	 * @param receiver
	 *            接收方
	 * @param title
	 * @param body
	 * @return
	 */
	public static boolean sendEmail(String receiver, String title, String body) {
		return sendEmail(new String[] { receiver }, title, body, false);
	}

	/**
	 * 
	 * 功能： 发mail的一个通用方法
	 * 
	 * @author: whh
	 * @Date: 2011-12-20
	 * @param receives
	 *            接收者
	 * @param title
	 *            标提
	 * @param mailContent
	 *            内容
	 * @param file
	 *            附件信息
	 */
	public static boolean sendEmail(String[] receives, String title, String mailContent, boolean html) {
		try {
			JavaMailSenderImpl sender = new JavaMailSenderImpl();
			sender.setUsername(showName);
			sender.setPassword(password);
			Properties props = System.getProperties();
			props.put("mail.smtp.host", TietiaoConstants.SEND_MAIL_SMTP_HOST);
			props.put("mail.smtp.localhost", TietiaoConstants.SEND_MAIL_SMTP_HOST);
			props.put("mail.smtp.auth", "true");
			sender.setJavaMailProperties(props);
			MimeMessage message = sender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(message, true, "gb2312");
			helper.setFrom(from, "贴条网");
			helper.setTo(receives);
			helper.setText(mailContent, html);
			helper.setSentDate(new Date());
			helper.setSubject(title);
			// 符件信息
			// if (file != null) {
			// helper.addAttachment(file.getName(), file);
			// }
			sender.send(message);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private static String getContentByPath(Map<String, Object> rootMap, String name) {
		try {
			Template template = cfg.getTemplate(name);
			Writer out = new StringWriter();
			template.process(rootMap, out);
			return out.toString();
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 
	 * 功能：发送邀请码
	 * 
	 * @author: whh
	 * @Date: 2011-12-21
	 */
	public static boolean sendInvite(String email, Map<String, Object> para) {
		para.put("nowTime", DateFormater.formatDateAndTime(new Date()));
		para.put("webHost", TietiaoConstants.TIETIAO_WEB_SITE);
		String content = getContentByPath(para, "txt_invite.ftl");
		return sendEmail(email, "您的好友邀请您加入贴条网", content);
	}

	/**
	 * 
	 * 功能：发送邀请码
	 * 
	 * @author: whh
	 * @Date: 2011-12-21
	 */
	public static boolean sendFindPass(String email, Map<String, Object> para) {
		para.put("nowTime", DateFormater.formatDateAndTime(new Date()));
		para.put("webHost", TietiaoConstants.TIETIAO_WEB_SITE);
		String content = getContentByPath(para, "txt_find_Password.ftl");
		return sendEmail(email, "您使用了找回密码功能", content);
	}

	public static void main(String[] args) {
		sendEmail("whh32@qq.com", "adsf", "whh32");
	}
}
